<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Model/Mapper/Collection.php
 * 
 * 
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 * @package GEM_Model
 * @subpackage GEM_Model_Mapper 
 */
class GEM_Model_Mapper_Collection implements Iterator,Countable
{
	/**
	 * Required to Lazy Load object collections.
	 * 
	 * Used to determine if the collection has
	 * actually been run. 
	 * 
	 * @var boolean
	 */
	protected $_run = false;
	
	/**
	 * Stores the values to be bound to the select
	 * statement.
	 * 
	 * @var array
	 */
	protected $_statementValues = array();
	
	/**
	 * Stores a prepared select statement.
	 * 
	 * @var PDOStatement
	 */
	protected $_selectStatement;
	
	/**
	 * Stores row data retrieved from database.
	 * 
	 * @var array
	 */
	protected $_rows = array();
	
	/**
	 * @var GEM_Model_Mapper_Mapper
	 */
	protected $_mapper;
	
	/**
	 * Collection Count
	 * 
	 * @var int
	 */
	protected $_count = 0;
	
	/**
	 * Array of domain objects.
	 * 
	 * @var GEM_Model_Domain_Object
	 */
	protected $_objects = array();
	
	/**
	 * Pointer to current position in $_objects array.
	 * 
	 * @var int
	 */
	protected $_position = 0;
	
	/**
	 * Constructor
	 * 
	 * @param GEM_Model_Mapper_Mapper $mapper The instantiating mapper object
	 * @param PDOStatement $selectStatement The statement to be executed when the collection is accessed
	 * @param array $statementValues | optional And array of SQL named parameters and their values
	 * @return void
	 */
	public function __construct(GEM_Model_Mapper_Mapper $mapper, PDOStatement $selectStatement, array $statementValues = null)
	{
		$this->_mapper = $mapper;
		$this->_selectStatement = $selectStatement;
		$this->_statementValues = $statementValues;
	}
	
	/**
	 * Returns the collection count. 
	 * 
	 * @return int
	 */
	public function count()
	{
		return $this->_run ? count($this->_objects) : count($this->_rows);
	}
	
	/**
	 * Returns the current key pointing to the current object.
	 * 
	 * @return int
	 */
	public function key()
	{
		return $this->_position;
	}
	
	/**
	 * Returns the current object (Not the pointer).
	 * 
	 * @return GEM_Model_Domain_Object
	 */
	public function current()
	{
		return $this->_getRow($this->_position);
	}
	
	/**
	 * Returns current object and increments the
	 * pointer to point to the next object if it exists.
	 * 
	 * @return GEM_Model_Domain_Object
	 */
	public function next()
	{
		$row = $this->_getRow($this->_position);
		if($row) $this->_position++;
		return $row;	
	}
	
	/**
	 * Resets pointer to point to element 0.
	 * 
	 * @return void
	 */
	public function rewind()
	{
		$this->_notifyAccess();
		$this->_position = 0;
	}
	
	/**
	 * Verifies that a value exists at the current pointer.
	 * 
	 * @return boolean
	 */
	public function valid()
	{
		return isset($this->_rows[$this->_position]);
	}	
	
	/**
	 * Loads the collection if it has not already been
	 * loaded.
	 * 
	 * @return void
	 */
	protected function _notifyAccess()
	{
		if($this->_run !== true){
			$this->_selectStatement->execute($this->_statementValues);
			$this->_rows = $this->_selectStatement->fetchAll();
			$this->_count = $this->count();
		}
		$this->_run = true;
	}
	
	/**
	 * Returns an object from the collection. If the object has not yet been
	 * instantiated, it is created.
	 * 
	 * @param int $rowNum
	 * @throws GEM_Model_Mapper_Exception
	 * @return GEM_Model_Domain_Object
	 */
	protected function _getRow($rowNum)
	{
		if(!is_int($rowNum))
			throw new GEM_Model_Mapper_Exception("Method " . __METHOD__ . " expects an integer.");		
		if($rowNum < 0 || $rowNum >= $this->_count)
			return null;
		if(isset($this->_objects[$rowNum]))
			return $this->_objects[$rowNum];
		if(isset($this->_rows[$rowNum])){
			$this->_objects[$rowNum] = $this->_mapper->createObject($this->_rows[$rowNum]);
			return $this->_objects[$rowNum];	
		}
	}
	
}